﻿#include "BrowserTableHandlerPhysicsFEProperty.h"
#include "OperatorsInterface/OperAbstractPhysics.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionShellHomogeneous.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

namespace GUI
{
    void BrowserTableHandlerPhysicsFEProperty::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("1") << tr("PID") << tr("3")
            << tr("4") << tr("5") << tr("6") << tr("7") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerPhysicsFEProperty::updateData()
    {
        if (_tableWidget == nullptr) return;

        Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        if (!disc)return;
        Acoustics::FITKAcousticsFEModel* feModel = disc->getFEModel();
        if (!feModel) return;

        //获取截面管理器
        Interface::FITKAbstractSectionManager *feMgr = feModel->getSectionManager();
        if (feMgr == nullptr) return;

        //数量
        const int n = feMgr->getDataCount();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为FE属性
            Interface::FITKAbaSectionShellHomogeneous* FE = dynamic_cast<Interface::FITKAbaSectionShellHomogeneous*>(feMgr->getDataByIndex(i));
            if (FE == nullptr) continue;
            //数据ID
            const int id = FE->getDataObjectID();

            QString materialName ="None";
            if (FE->getMaterial())materialName = FE->getMaterialName();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(FE->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem("PSHELL", id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(id), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(materialName, id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(FE->getThickness()), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractPhysics * BrowserTableHandlerPhysicsFEProperty::getPhysicsPropOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractPhysics>("OperPhysicsPropFE");
    }

}
